package com.zxx.googlemap;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.CompositePageTransformer;
import androidx.viewpager2.widget.MarginPageTransformer;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import javax.xml.transform.Transformer;
//ViewPager2的使用
public class MainActivity extends AppCompatActivity {

    private ViewPager2 viewPager2;
    private ViewPager2 vp_fragment;

    private boolean isSlide;
    private List<Integer> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager2 = findViewById(R.id.view_pager2);
        initPage();

        vp_fragment = findViewById(R.id.vp_fragment);
        initFragment();
    }

    private void initFragment() {
        //9.在Activity中为ViewPager2设置FragmentStateAdapter
        vp_fragment.setAdapter(new AdapterFragmentPager(this));
        vp_fragment.setOffscreenPageLimit(3);
        vp_fragment.setUserInputEnabled(isSlide);
    }

    private void initPage() {
        ViewPager2Adapter adapter = new ViewPager2Adapter();

        data = new ArrayList<>();
        data.add(0);
        data.add(1);
        data.add(2);
        data.add(3);
        adapter.setList(data);
        viewPager2.setAdapter(adapter);

        //1.默认水平功能
        //2.设置竖直功能
//        viewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);

        //3.设置监听
        //我们为ViewPager设置页面滑动的监听事件需要重写三个方法，而为ViewPager2设置监听事件只需要重写需要的方法即可，
        // 因为ViewPager2中OnPageChangeCallback是一个抽象类。
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Toast.makeText(MainActivity.this, "Select:" + position, Toast.LENGTH_SHORT).show();
                if (position == 1 || position == 3) {
                    isSlide = true;
                } else {
                    isSlide = false;
                }
                vp_fragment.setUserInputEnabled(isSlide);
            }
        });

        //4.禁止滑动
        //在使用ViewPager的时候想要禁止用户滑动需要重写ViewPager的onInterceptTouchEvent。
        //而ViewPager2被声明为了final，我们无法再去继承ViewPager2。
//        viewPager2.setUserInputEnabled(false);

        //5.fakeDragBy模拟拖拽，接受一个float的参数，当参数值为正数时表示向前一个页面滑动，当值为负数时表示向下一个页面滑动。
//        fakeDrag(-310f);

        //6.设置间距:MarginPageTransformer
        viewPager2.setPageTransformer(new MarginPageTransformer((int) getResources().getDimension(R.dimen.dp_10)));

        //7.组合Transformer
        //CompositePageTransformer实现了PageTransformer接口，同时在其内部维护了一个List集合，我们可以将多个PageTransformer添加到CompositePageTransformer中。
        CompositePageTransformer transformer = new CompositePageTransformer();
        transformer.addTransformer(new ScaleInTransformer());
        transformer.addTransformer(new MarginPageTransformer(10));
        viewPager2.setPageTransformer(transformer);

        //8.一屏多页
        viewPager2.setOffscreenPageLimit(1);
        RecyclerView recyclerView = (RecyclerView) viewPager2.getChildAt(0);
        int padding = getResources().getDimensionPixelOffset(R.dimen.dp_10)
                + getResources().getDimensionPixelOffset(R.dimen.dp_10);
        recyclerView.setPadding(padding, 0, padding, 0);
        recyclerView.setClipToPadding(false);

        //Kotlin 写法：
        //https://www.jianshu.com/p/e7abbfb109ac  apply返回自己
//        viewPager2.apply {
//            offscreenPageLimit=1
//            val recyclerView= getChildAt(0) as RecyclerView
//            recyclerView.apply {
//                val padding = resources.getDimensionPixelOffset(R.dimen.dp_10) +
//                        resources.getDimensionPixelOffset(R.dimen.dp_10)
//                setPadding(padding, 0, padding, 0)
//                clipToPadding = false
//            }
//        }
    }

    //通过按钮点击可以模拟用户滑动。
    private void fakeDrag(float offset) {
        viewPager2.beginFakeDrag();
        if (viewPager2.fakeDragBy(offset)) {
            viewPager2.endFakeDrag();
        }
    }


    class ScaleInTransformer implements ViewPager2.PageTransformer {

        private static final float DEFAULT_MIN_SCALE = 0.85f;
        private static final float DEFAULT_CENTER = 0.5f;
        private float mMinScale = DEFAULT_MIN_SCALE;

        @Override
        public void transformPage(@NonNull View page, float position) {
//            int pageWidth = page.getWidth();
//            int pageHeight = page.getHeight();
//
//            page.setPivotX(pageWidth / 2);
//            page.setPivotY(pageHeight / 2);
//
//            float abs = Math.abs(position);
//
//            page.setScaleX((float) (1.5 - (0.15 * abs)));
//            page.setScaleY((float) (1.5 - (0.15 * abs)));
//            page.setBackgroundResource(R.drawable.ic_launcher_background);
//            page.setTranslationY(-50 * abs);

            //----上面错误的
            float abs = Math.abs(position);
            page.setElevation(-abs);
            int pageWidth = page.getWidth();
            int pageHeight = page.getHeight();

            page.setPivotX(pageWidth / 2f);
            page.setPivotY(pageHeight / 2f);

            if (position < -1) {
                page.setScaleX(mMinScale);
                page.setScaleY(mMinScale);
                page.setPivotX(pageWidth);
            } else if (position <= 1) {
                if (position < 0) {
                    float scaleFactor = (1 + position) * (1 - mMinScale) + mMinScale;
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                    page.setPivotX(pageWidth * (DEFAULT_CENTER + DEFAULT_CENTER * -position));
                } else {
                    float scaleFactor = (1 - position) * (1 - mMinScale) + mMinScale;
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                    page.setPivotX(pageWidth * ((1 - position) * DEFAULT_CENTER));
                }
            } else {
                page.setPivotX(0f);
                page.setScaleX(mMinScale);
                page.setScaleY(mMinScale);
            }
        }
    }

}
